package com.ilop.sthome.ui.activity.detail.common;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.ui.adapter.detail.MappingAdapter;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityChooseMappingBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020/4/15.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：选择应用场景
 */
public class ChooseMappingActivity extends BaseBindActivity<ActivityChooseMappingBinding> {

    private String mDeviceName;
    private String mMapping;
    private int mSceneSize;
    private MappingAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_mapping;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mMapping = getIntent().getStringExtra(CommonId.KEY_SCENE_ID);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new MappingAdapter(mContext, mDeviceName, mMapping);
        mDBind.rvSceneList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        List<SceneBean> mList = SceneDaoUtil.getInstance().findAllScene(mDeviceName);
        if (mList.size()>0){
            showSceneList(mList);
        }else {
            withOutScene();
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivMappingBack.setOnClickListener(v -> finish());
        mDBind.tvSaveMapping.setOnClickListener(v -> {
            List<String> sceneList = mAdapter.getSceneList();
            if (sceneList == null || sceneList.size() == 0){
                showToast(getString(R.string.please_choose_mode));
                return;
            }
            LiveDataBus.get().with(CommonId.KEY_SUCCESS).setValue(sceneList);
            finish();
        });
    }

    private void showSceneList(List<SceneBean> mList) {
        this.mSceneSize = mList.size();
        mAdapter.setList(mList);
    }

    private void withOutScene() {
        mAdapter.setList(null);
        mDBind.rvSceneList.setEmptyView(getString(R.string.no_data), R.mipmap.scene_empty);
    }
}
